//
//  YMDiscoverSearchView.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/22.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit

class YMDiscoverSearchView: UIView {

    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 35))
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI () {
        
        addSubview(cancelButton)
        addSubview(textField)
        
        cancelButton.snp_makeConstraints { (make) in
            make.width.equalTo(50)
            make.top.bottom.trailing.equalTo(self)
            
        }
       
        textField.snp_makeConstraints { (make) in
            make.top.leading.bottom.equalTo(self)
            make.trailing.equalTo(self)
        }
        
    }
    
    
    
    
    
    
    func buttonClick() {
        
        self.textField.snp_updateConstraints { (make) in
            make.trailing.equalTo(self)
        }
        
        UIView.animateWithDuration(0.25, animations: { 
            
            self.layoutIfNeeded()
            }) { (_) in
            self.textField.resignFirstResponder()
        }
        
    }
    
    
    
    
    //懒加载控件
    
    //按钮
    lazy var cancelButton:UIButton = {
       
        let button = UIButton()
        button.addTarget(self, action: #selector(YMDiscoverSearchView.buttonClick), forControlEvents: .TouchUpInside)
        button.backgroundColor = UIColor.orangeColor()
        button.setTitle("取消", forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        
        return button
    }()
    
    
    //输入框
    lazy var textField:UITextField = {
       
        let txt = UITextField()
        txt.placeholder = "上海传智三期"
        txt.layer.borderColor = ThemeColor.CGColor
        txt.layer.borderWidth = 2
        txt.backgroundColor = UIColor.whiteColor()
        
        let img = UIImageView(imgName: "tabbar_profile")
        img.contentMode = .Center
        img.frame.size = CGSize(width: 35, height: 35)
        
        txt.leftView = img
        txt.leftViewMode = .Always
        txt.delegate = self
        
        return txt
        
    }()
    
    
    
}


extension YMDiscoverSearchView:UITextFieldDelegate {
    
    func textFieldDidBeginEditing(textField: UITextField) {
        
        self.textField.snp_updateConstraints { (make) in
            make.trailing.equalTo(self).offset(-50)
        }
        
        UIView.animateWithDuration(0.25) { 
            self.layoutIfNeeded()
        }
    }
    
}











